package xyz.bali16.module.blog.controller;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.module.blog.utils.PageUtils;
import xyz.bali16.module.blog.entity.Tag;
import xyz.bali16.module.blog.model.vo.blog.TagVo;
import xyz.bali16.module.blog.service.ArticleTagService;
import xyz.bali16.module.blog.service.TagService;


import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(BaseController.API + "blog/tag")
public class TagController extends BaseController {
    @Autowired
    private TagService tagService;


    @GetMapping("/getLeftAsideFix")
    @ResponseBody
    @ApiOperation("获取左侧栏固定标签")
    //@PreAuthorize("@auth.permission('plugin:blog:tag:query')")
    public Result<List<TagVo>> getLeftAsideFix() {
        return Result.success(tagService.getLeftAsideFixTag());
    }

    @GetMapping("/list")
    @ResponseBody
    @ApiOperation("标签列表数据")
    //@PreAuthorize("@auth.permission('plugin:blog:tag:query')")
    public Result<PageUtils> list(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                  @RequestParam(value = "size", required = false, defaultValue = "20") Integer size) {
        return Result.success(tagService.queryPage(current, size));
    }

    @RequestMapping("/add")
    @ResponseBody
    //@PreAuthorize("@auth.permission('plugin:blog:tag:add')")
    public Result<Boolean> save(@RequestBody Tag tagForm) {
        boolean state = tagService.save(tagForm);
        return Result.success(state);
    }

    @PostMapping("/edit")
    @ResponseBody
    //@PreAuthorize("@auth.permission('plugin:blog:tag:update')")
    public Result<Boolean> edit(@RequestBody Tag tagForm) {
        System.out.println("edit:tag");
        System.out.println(tagForm.toString());
        return Result.success(tagService.updateById(tagForm));
    }

    /**
     * 查询标签详情
     */
    @GetMapping("/detail")
    //@PreAuthorize("@auth.permission('plugin:blog:tag:query')")
    public Result<List<TagVo>> detail() {
        List<TagVo> tagVos = tagService.queryTagDetails();
        return Result.success(tagVos);
    }

    /**
     * 单条标签详情
     */
    @GetMapping("/{tagId}")
    //@PreAuthorize("@auth.permission('plugin:blog:tag:query')")
    public Result<TagVo> detailById(@PathVariable("tagId") Long tagId) {
        TagVo tagVo = tagService.queryOneTagDetail(tagId);
        return Result.success(tagVo);
    }


    @PostMapping("/delete")
    @ResponseBody
    //@PreAuthorize("@auth.permission('plugin:blog:tag:delete')")
    public Result delete(@RequestBody Map<Object, String> map) {
        Long id = Long.valueOf(map.get("id"));
        return Result.success(tagService.removeById(id));
    }
}
